<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        <div>
          <el-button type="primary" @click="handleAdd">
            <i class="el-icon-circle-plus-outline"></i>增加
          </el-button>
          <el-button type="danger" @click="handleDeleteAll">
            <i class="el-icon-delete"></i>删除
          </el-button>
        </div>
        <div class="flex mt-3">
          <el-input
            v-model="listQuery.gift_name"
            class="mr-4"
            placeholder="请输入礼物名称"
            style="width: 17%"
          ></el-input>
          <el-select
            v-model="listQuery.sale_status"
            placeholder="操作类型"
            style="width: 17%"
          >
            <el-option
              v-for="item in sale_options"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <div class="ml-4 flex items-center" style="width: 17%">
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button type="primary" @click="handleReset">刷新</el-button>
          </div>
        </div>
      </div>
      <el-table
        :data="menuList"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="序号" prop="id"></el-table-column>
        <el-table-column label="礼物名称" prop="name"></el-table-column>
        <el-table-column label="礼物价格" prop="price"></el-table-column>
        <el-table-column label="礼物图片" prop="picture">
          <template slot-scope="scope">
            <el-popover max-width="420" placement="top" trigger="hover">
              <img
                :src="scope.row.picture"
                alt=""
                style="max-width: 200px; max-height: 200px"
              />
              <img
                slot="reference"
                :src="scope.row.picture"
                alt=""
                style="width: 50px"
              />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="礼物动效" prop="special">
          <template slot-scope="{ row }">
            <el-popover
              v-if="/\.(png|jpg|gif|jpeg|webp)$/.test(row.special)"
              max-width="420"
              placement="top"
              trigger="hover"
            >
              <img
                :src="row.special"
                alt=""
                style="max-width: 200px; max-height: 200px"
              />
              <img
                slot="reference"
                :src="row.special"
                alt=""
                style="width: 50px"
              />
            </el-popover>
            <el-popover
              v-else-if="/\.mp4$/.test(row.special)"
              max-width="420"
              placement="top"
              trigger="hover"
            >
              <video
                :src="row.special"
                controls
                style="max-width: 200px; max-height: 200px"
              />
              <video
                slot="reference"
                :src="row.special"
                autoplay
                loop
                muted
                style="width: 50px"
              />
            </el-popover>
            <p v-else>{{ row.special }}</p>
          </template>
        </el-table-column>
        <el-table-column label="升级数量" prop="upgrade_count"></el-table-column>
        <el-table-column label="状态" prop="">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.sold === 1" type="primary">上架</el-tag>
            <el-tag v-else type="danger">下架</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="心动值" prop="cardiac"></el-table-column>
        <el-table-column label="PK值" prop="pk_score"></el-table-column>
        <el-table-column label="全服广播" prop="broadcast_name"></el-table-column>
        <el-table-column label="礼物尺寸" prop="size"></el-table-column>
        <el-table-column label="是否展示礼物墙">
          <template slot-scope="{ row }">
            <el-tag v-if="row.is_show_in_wall === 1" type="primary">是</el-tag>
            <el-tag v-else type="info">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="300">
          <template slot-scope="scope">
            <el-button
              plain
              size="mini"
              type="success"
              @click="handleEdit(scope.row)"
            >
              <i class="el-icon-edit-outline"></i>编辑
            </el-button>
            <el-dropdown class="ml-3">
              <el-button type="primary">
                更多操作<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleChangeStatus(scope.row)">
                  {{ scope.row.sold === 1 ? '下架' : '上架' }}
                </el-dropdown-item>
                <el-dropdown-item
                  @click.native="handleStatusCrontab(scope.row)"
                >
                  定时任务
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleSetTop(scope.row)">
                  置顶
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleSend(scope.row)">
                  赠送
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleDeleteItem(scope.row)">
                  删除
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页 -->
    <cus-pagination
      :limit.sync="listQuery.page_size"
      :page.sync="listQuery.page"
      :total="total"
      @pagination="handleGetData"
    />

    <!-- 删除弹框 -->
    <el-dialog
      :visible.sync="deleteDialogVisible"
      title="确定删除吗?"
      width="435px"
    >
      <span style=""
      ><em style="font-style: normal; color: red">删除后不可恢复</em
      >，你还要继续吗？</span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleDelete">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 设置定时上下架 -->
    <el-dialog
      :before-close="handleBeforeCloseStatusCrontab"
      :visible.sync="statusCrontabVisible"
      title="这是定时上下架?"
      width="435px"
    >
      <el-form
        ref="statusCrontabForm"
        :model="statusCrontabForm"
        :rules="statusCrontabRules"
        label-width="120"
      >
        <el-form-item label="操作" prop="action">
          <el-radio v-model="statusCrontabForm.action" :label="1">
            上架
          </el-radio>
          <el-radio v-model="statusCrontabForm.action" :label="0">
            下架
          </el-radio>
        </el-form-item>
        <el-form-item label="设置时间" prop="set_time">
          <el-date-picker
            v-model="statusCrontabForm.set_time"
            placeholder="选择日期时间"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="statusCrontabVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="handleConfirmStatusCrontab('statusCrontabForm')"
        >
          确 定
        </el-button>
      </span>
    </el-dialog>

    <addEditModal
      :id="rowData.id ? rowData.id : -1"
      :dialogVisible.sync="dialogVisible"
      :higherId="id"
      :title.sync="dialogTitle"
      @updateTable="handleSearch"
    ></addEditModal>

    <sendModal
      :id="sendRowData.id ? sendRowData.id : -1"
      :dialogVisible.sync="sendVisible"
      :higherId="id"
    ></sendModal>
  </div>
</template>

<script>
import { deleteData, getData, postChangeStatus, postSetTop, postStatusCrontab } from '@/api/roomGift/giftList'

import addEditModal from './childComps/addEditModal'
import sendModal from './childComps/sendModal.vue'

export default {
  name: '',
  components: {
    addEditModal,
    sendModal
  },
  data() {
    return {
      sale_options: [
        {
          id: -1,
          title: '全部'
        },
        {
          id: 0,
          title: '下架'
        },
        {
          id: 1,
          title: '上架'
        }
      ],
      listQuery: {
        gift_name: undefined,
        sale_status: -1,
        page: 1,
        page_size: 10,
        higher_id: undefined
      },
      menuList: [],
      total: 0,
      multipleSelection: [],
      dialogTitle: 'add',

      rowData: {},
      delete_ids: undefined,
      dialogVisible: false,

      deleteDialogVisible: false,

      sendVisible: false,
      sendRowData: {},

      /** 设置定时上下架 */
      statusCrontabVisible: false,
      statusCrontabForm: {
        id: undefined,
        action: undefined,
        set_time: undefined
      },
      statusCrontabRules: {
        action: [{ required: true, message: '请选择操作', trigger: 'change' }],
        set_time: [
          { required: true, message: '请选择定时时间', trigger: 'change' }
        ]
      },
      id: undefined
    }
  },
  created() {
    this.id = this.$route.query.id
    this.listQuery.higher_id = this.$route.query.id
    this.handleGetData()
  },
  methods: {
    /**获取首页数据 */
    handleGetData() {
      getData(this.listQuery).then(res => {
        this.menuList = res.data.data
        this.total = res.data.total
      })
    },
    /**搜索 */
    handleSearch() {
      this.listQuery.page = 1
      this.handleGetData()
    },
    /**重置 */
    handleReset() {
      this.listQuery = {
        gift_name: undefined,
        sale_status: -1,
        page: 1,
        page_size: 10,
        higher_id: this.id
      }
      this.handleGetData()
    },
    /**选择单选框 */
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /**添加 */
    handleAdd() {
      this.dialogTitle = 'add'
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.rowData = row
      this.dialogTitle = 'edit'
      this.dialogVisible = true
    },
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请勾选要删除的数据')
        return
      } else {
        let arr = []
        this.multipleSelection.forEach(item => {
          arr.push(item.id)
        })
        this.delete_ids = arr.join(',')
      }
      this.deleteDialogVisible = true
    },
    handleDeleteItem(row) {
      this.delete_ids = row.id.toString()
      this.deleteDialogVisible = true
    },
    handleDelete() {
      deleteData(this.delete_ids).then(res => {
        if (res.code === 200) {
          this.handleGetData()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        }
      })
    },
    handleSend(row) {
      this.sendRowData = row
      this.sendVisible = true
    },

    /**设置定时上下架 */
    handleStatusCrontab(row) {
      this.statusCrontabVisible = true
      this.statusCrontabForm.id = row.id
    },
    handleConfirmStatusCrontab(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          postStatusCrontab(this.statusCrontabForm).then(res => {
            if (res.code === 200) {
              this.handleReset()
              this.statusCrontabVisible = false
              this.$notify({
                type: 'success',
                message: '操作成功',
                position: 'bottom-right',
                showClose: false,
                duration: 3000
              })
            }
          })
        } else {
          return false
        }
      })
    },
    handleBeforeCloseStatusCrontab() {
      this.statusCrontabForm = {
        id: undefined,
        action: undefined,
        set_time: undefined
      }
    },

    /**置顶 */
    handleSetTop(row) {
      postSetTop(row.id).then(res => {
        if (res.code === 200) {
          this.handleGetData()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        }
      })
    },
    /**切换上下架 */
    handleChangeStatus(row) {
      postChangeStatus(row.id).then(res => {
        if (res.code === 200) {
          this.handleGetData()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped></style>
